<?php
/**
 * Created by PhpStorm.
 * User: tt
 * Date: 2017/12/25
 * Time: 15:00
 */

namespace app\admin\controller;
use app\admin\logic\Store as Store_logic;
use think\Db;


class Store extends Admin
{

    /**
     * 门店列表
     * @return mixed
     */
    public function index(){
        $store = Db::name('store')->select();
        foreach($store as $k=>$v){
            $store[$k]['depar'] = Db::name('depar')->where('id',$v['depar_id'])->field('name')->find();
            $store[$k]['area'] = Db::name('area')->where('id',$v['area_id'])->field('name')->find();
        }
        $this->assign('store',$store);
        return $this->fetch();
    }

    /**
     * 新增门店
     * @return mixed
     */
    public function add(){
        $depar = Db::name('depar')->where('status',1)->field('id,name,pid')->select();
        $area = Db::name('area')->where('status',1)->field('id,name,type')->select();
        $this->assign('depar',$depar);
        $this->assign('area',$area);
        $data = $this->request->param();
        if($this->request->isPost()){
            $res = Db::name('store')->insert($data);
            if($res == 1){
                $this->success('新增成功');
            }else{
                $this->error('新增失败');
            }

        }
        return $this->fetch();
    }


    /**
     * 编辑门店
     * @return mixed
     */
    public function edit(){
        $depar = Db::name('depar')->where('status',1)->field('id,name')->select();
        $area = Db::name('area')->where('status',1)->field('id,name')->select();
        $this->assign('depar',$depar);
        $this->assign('area',$area);
        $data = $this->request->param();
        if($this->request->isPost()){

            Db::name('store')->where(['id'=>$data['id']])->update($data);
            $this->success('修改成功');
        }else{
            $id=input('id');
            $store = Db::name('store')->where('id',$id)->find();
            $this->assign('store',$store);
            return $this->fetch();
        }
        return $this->fetch();
    }


    /**
     * 删除门店
     * @return mixed
     */
    public function del(){
        $id = $this->request->param('id', 0);
        $Store_logic = new Store_logic();
        $result = $Store_logic->delStore($id);
        if ($result['result'] === 1) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
}